import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import 'view-design/dist/styles/iview.css'
import { Message, Notice, Modal } from 'view-design'

import Request from './utils/request'
import auth from './service/auth'

Vue.config.productionTip = false

Vue.prototype.$request = Request
Vue.prototype.$Message = Message
Vue.prototype.$Notice = Notice
Vue.prototype.$Modal = Modal

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (!to.meta.direct && store.getters.account === undefined) {
    // 验证用户信息
    if (auth.auth()) {
      auth.user().then((resp) => {
        store.dispatch('login', resp.data).then(r => {})
        next()
      }).catch(() => {
        store.dispatch('logout').then(r => {})
        auth.flush()
        next({ path: '/login' })
      })
    } else {
      next({ path: '/login' })
    }
  } else {
    if (to.name === 'login' && auth.auth()) { // 已经登陆
      next({ path: '/' })
    } else {
      next()
    }
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
